<?php
// 应用公共文件

function time_str($time){
    $time_stamp = strtotime($time);
    $now_stamp = time();
    if($time_stamp<=$now_stamp){
        $time_stamp = strtotime(substr($time,0,10).' 00:00:00');
        $distance= $now_stamp-$time_stamp;
        if(floor($distance/(60*60*24))){
            $day = floor($distance/(60*60*24));
            
            if($day>1){
                if($day>2){
                    return $time;
                }
                return ($day-1).'天前';
            }else{
                return '昨天';
            }
        }
        $time_stamp = strtotime($time);
        $distance= $now_stamp-$time_stamp;
        if($distance<(60*60*24)){
            if($distance>=60*60){
                return floor($distance/(60*60)).'小时前';
            }
            if($distance>=60){
                return floor($distance/60).'分钟前';
            }
            return '刚刚';
            // if($distance<60){
            //     return $distance.'秒前';
            // }
        }

    }else{
        $time_stamp = strtotime(substr($time,0,10).' 00:00:00');
        $distance= $time_stamp - $now_stamp;
        if(floor($distance/(60*60*24))){
            $day = floor($distance/(60*60*24));
            if($day>1){
                if($day>2){
                    return $time;
                }
                return ($day-1).'天后';
            }else{
                return '明天';
            }
        }
        $time_stamp = strtotime($time);
        $distance= $now_stamp-$time_stamp;
        if($distance<(60*60*24)){
            if($distance>=60*60){
                return floor($distance/(60*60)).'小时后';
            }
            if($distance>=60){
                return floor($distance/60).'分钟后';
            }
            return '几秒后';
            // if($distance<60){
            //     return $distance.'秒前';
            // }
        }
        
    }
    
}